import styles from '../index.module.less';
import { tradeListFields } from '@/utils/helper';
import { useEffect, useRef, useState } from 'react';
import { ProTable, ProColumns } from '@ant-design/pro-components';
import FilterSelect from './FilterSelect';
import { getOrderByUser, getUsdtNum, handleDirection, isSuccess } from '@/utils';
import { tradeListApi } from '@/api';
import dayjs from 'dayjs';

export default function TradeList({ tableWidth }: any) {
  const [list, setList] = useState<any>([]);
  const tableRef: any = useRef('');
  const [loading, setLoading] = useState(false);

  useEffect(() => {}, []);

  // 请求:成交记录
  async function getData(val: any) {
    const params: any = {
      ...val,
      contract: '',
    };
    if (val?.role === 'all') delete params.role;
    setLoading(true);
    const res: any = await tradeListApi(params);
    setLoading(false);
    if (isSuccess(res)) {
      const arr = getOrderByUser(
        res.data.map((item: any, i: number) => ({ ...item, idOld: item.id, id: i + 1 })),
      );
      const _list =
        val.side === 'all'
          ? arr
          : arr.filter((item: any) => (val.side === 'duo' ? item.size > 0 : item.size < 0));
      setList(_list);
    }
  }

  const columns: ProColumns[] = [
    {
      title: '合约',
      dataIndex: 'contract',
    },
    {
      title: '方向',
      dataIndex: 'newSize',
      render: (text: any, record: any) => (
        <div className={`${text > 0 ? 'color-red' : 'color-green'}`}>
          {text > 0 ? '做多' : '做空'}
        </div>
      ),
    },
    {
      title: '委托ID',
      dataIndex: 'order_id',
    },
    {
      title: '数量(USDT)',
      dataIndex: 'size',
      render: (text: any, record: any) => <div>{getUsdtNum(record)}</div>,
    },
    {
      title: '成交均价',
      dataIndex: 'price',
    },
    {
      title: '角色',
      dataIndex: 'role',
    },
    {
      title: '手续费',
      dataIndex: 'fee',
      render: (text: any) => <div>{text} USDT</div>,
    },
    {
      title: '成交时间',
      dataIndex: 'create_time',
      render: (text: any) => <>{dayjs(text * 1000).format('YYYY-MM-DD HH:mm:ss')}</>,
    },
  ];

  return (
    <>
      <FilterSelect fields={tradeListFields} reload={getData} />
      <ProTable
        rowKey="id"
        columns={columns}
        dataSource={list}
        actionRef={tableRef}
        search={false}
        options={false}
        scroll={{ x: tableWidth }}
        pagination={false}
        loading={loading}
      />
      <div className={`${styles.xxx} xxx`}></div>
    </>
  );
}
